package com.examination.controller.integralModel;


import com.examination.entity.Conversion;
import com.examination.entity.Staff;
import com.examination.service.integralModel.ConversionSService;
import com.examination.service.staffModel.StaffService;
import com.examination.util.Msg;
import com.examination.util.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Api(description = "兑换接口")
@RestController
@RequestMapping("conversion")
@CrossOrigin
public class ConversionSController {
    @Autowired
    private ConversionSService conversionSService;

    @Autowired
    private StaffService staffService;

    @ApiOperation(value = "增加兑换记录" , notes = "")
    @PostMapping("InsertConversion")
    public Msg InsertConversion(@RequestBody Map map) {
        map.put("staffId",map.get("id"));
        Msg msg = new Msg();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = new Date();
        String time = format.format(now);
        map.put("conversionTime", time);
        Staff staff = staffService.FindStaffBySid(map);
        String commodityPriceS = (String) map.get("commodityPrice");
        double commodityPrice = Double.valueOf(commodityPriceS.toString());
        if(staff!=null){
        if (staff.getEnd_integral() >= commodityPrice) {
        int i = conversionSService.insertConversion(map);
            if (i == 1) {
                staffService.updateEndIntegral(map);
                msg.setMessage("兑换成功!");
                msg.setCode(200);
                return msg;
            } else {
                msg.setMessage("兑换失败!");
                msg.setCode(20006);
                return msg;
            }
        } else {
            msg.setMessage("积分不足兑换失败!");
            msg.setCode(20005);
            return msg;
        }
        }else {
            msg.setMessage("用户信息不正确!");
            msg.setCode(20007);
            return msg;
        }
    }

    @ApiOperation(value = "分页模糊查询兑换记录")
    @PostMapping("findConversion")
    public Page<Conversion> findConversion(@RequestBody Map map) {
        Page<Conversion> page = new Page<Conversion>();
        page.setPageNo((Integer) map.get("pageNo"));
        page.setPageSize((Integer) map.get("pageSize"));
        page.setItems(conversionSService.findConversion(map));
        page.setTotal(conversionSService.Total(map));
        return page;
    }

    @ApiOperation(value = "分页查找个人兑换记录")
    @PostMapping("findConversionBySid")
    public Page<Conversion> findConversionBySid(@RequestBody Map map) {
        Page<Conversion> page = new Page<Conversion>();
        page.setPageNo((Integer) map.get("pageNo"));
        page.setPageSize((Integer) map.get("pageSize"));
        page.setItems(conversionSService.findConversionBySid(map));
        page.setTotal(conversionSService.Total(map));
        return page;
    }
}
